﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 单选框
/// </summary>
public class GUIToggleGroup : MonoBehaviour
{
    public GUIToggle[] toggles;
    private GUIToggle preToggle;

    private void Start()
    {
        for (int i = 0; i < toggles.Length; i++)
        {
            //为了形成闭包，必须先拿出来装起来
            GUIToggle toggle = toggles[i];
            toggle.toggleEvent += (value) =>
            {
                //如果当前单选框为true，则其他单选框为false
                if (value)
                {
                    preToggle = toggle;
                    for (int j = 0; j < toggles.Length; j++)
                    {
                        if (toggles[j] != toggle)
                        {
                            toggles[j].ChangeState(false);
                        }
                    }
                }
                else
                {
                    if (preToggle == toggle)
                    {
                        toggle.ChangeState(true);
                    }
                }
            };
        }
    }
}
